package com.weilan.ark3.controller;

import com.weilan.ark3.buffer.DataArray;
import com.weilan.ark3.buffer.DataArray2;
import com.weilan.ark3.buffer.MsgQueue;
import com.weilan.ark3.buffer.OverallSituation;
import com.weilan.ark3.common.Communication;
import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.common.Result;
import com.weilan.ark3.entity.*;
import com.weilan.ark3.sqlite.SQLPVTimeUtils;
import com.weilan.ark3.sqlite.SQLiteConfigUtils;
import com.weilan.ark3.sqlite.SQLiteParamUtils;
import com.weilan.ark3.utils.ArrayUtils;
import com.weilan.ark3.utils.DateUtils;
import com.weilan.ark3.utils.HexUtils;
import com.weilan.ark3.utils.ammeterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static com.weilan.ark3.buffer.OverallSituation.OverallEMSData;
import static com.weilan.ark3.buffer.OverallSituation.controlId;

@RestController
@CrossOrigin
@RequestMapping("weilan/system/PV")
public class PvController {
    //Logger test = LoggerFactory.getLogger("HTTPServer");

    /*
     * 光伏自发自用
     * */
    public static void Spontaneous_pv() {
        //用于第一次降功率
        if (GlobalParam.pvZero == 0) {
            ControlDto controlDto = new ControlDto();
            controlDto.setSlaveId(255);
            controlDto.setAddress(302);
            controlDto.setValue(0);
            MsgQueue.writeToControlTcp(controlDto);
            GlobalParam.backZero = 0;
            GlobalParam.fengguZero = 0;
            GlobalParam.pvZero = 1;

            //取消禁充标志
            GlobalParam.bansign = 0;
        }
        //1.获取参数
        OverallEMSData = DataArray.slaveDataMap.get(1);
        //int mode = OverallEMSData[60];
        //从数据库中查数据
        ParamDto query = SQLiteParamUtils.query();
        int pv_mode = query.getPv_mode();
        ControlDto controlEMS = new ControlDto();
        ControlDto controlPV = new ControlDto();
        //1.判断系统当前的模式
        switch (pv_mode) {
            case 7:
                //光伏并网策略
                on_ToGrid(controlEMS, controlPV);
                break;
            case 8:
                //光伏+时段(光伏可控)策略
                on_ToGrid_Time(controlEMS, controlPV);
                break;
            case 9:
                //光伏+时段(光伏不可控)策略
                on_ToGrid_Time_noPV(controlEMS);
                break;
            case 10:
                //光伏(直流MPPT)
                on_ToGrid_MPPT(controlEMS, controlPV);
            case 11:
                //控制断路器
                on_Grid_breaker(controlEMS, controlPV);
                break;
            case 12:
                //光伏优先储能
                PV_priority_ESS(controlEMS, controlPV);
                break;
            case 13:
                //光伏不可控(无时段)
                PV_Uncontroll_ESS(controlEMS);
                break;
            default:
                break;
        }

    }

    /*
     * 光伏优先储能
     * */
    public static void PV_priority_ESS(ControlDto controlEMS, ControlDto controlPV) {
        ParamDto param = SQLiteParamUtils.query();
        ParamDto query = SQLiteParamUtils.query();
        Integer antiBackflow = query.getAnti_reflux();
        int soc_h = Integer.parseInt(param.getSoc_h());
        int soc_l = Integer.parseInt(param.getSoc_l());
        int pv_max = param.getPv_max() * 10;
        int allowance = param.getGrid_alw() * 10;
        int stepSize = param.getStep_size() * 10;
        PvCtlDto pvCtlDto = new PvCtlDto();
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short pvC_status = Communication.pvm_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[63];//PHC系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        short PV_Power = OverallEMSData[14003];//光伏实际输出功率
        short PV_Max_Power = (short) pv_max;//光伏额定输出功率
        short pvm_status = OverallEMSData[14000];//光伏逆变器的状态
        //电网功率
        short Grid_meter_power = ammeterUtils.getGrid_Power();
        System.out.println("电网功率:" + Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1 + "光伏功率" + PV_Power * 0.1);
        //允许充电
        pvCtlDto.setAllow_charge(OverallEMSData[5]);
        //允许放电
        pvCtlDto.setAllow_discharge(OverallEMSData[4]);
        //电网功率
        pvCtlDto.setGrid_power(Grid_meter_power);
        //高压保护
        pvCtlDto.setH_alarm(OverallEMSData[16]);
        //低压保护
        pvCtlDto.setL_alarm(OverallEMSData[17]);
        //光伏现在功率
        pvCtlDto.setPv_power(OverallEMSData[368]);
        //光伏额定功率
        pvCtlDto.setPv_rated_power(pv_max);
        //系统soc
        pvCtlDto.setSys_soc(OverallEMSData[26]);
        //系统指令功率
        pvCtlDto.setSys_cmd(OverallEMSData[63]);
        //容差
        pvCtlDto.setAllow_range(allowance);
        //soc_h
        pvCtlDto.setSoc_h(soc_h);
        //soc_l
        pvCtlDto.setSoc_l(soc_l);
        //步长
        pvCtlDto.setStep_size(stepSize);
        if (antiBackflow == 1) {//防逆流
            if (meter_com_status == 1) {
                //储能停机
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(301);
                controlEMS.setValue(GlobalParam.stop);
                MsgQueue.writeToControlTcp(controlEMS);

                //光伏0功率
                controlPV.setSlaveId(1);
                controlPV.setAddress(368);
                controlPV.setValue(0);
                MsgQueue.writeToControlPV(controlPV);
            } else {
                if (sys_status == 3) {
                    int power = pvCtlDto.getGrid_power() + PV_Power - pvCtlDto.getAllow_range();
                    //光伏0功率
                    controlPV.setSlaveId(1);
                    controlPV.setAddress(368);
                    controlPV.setValue(power);
                    MsgQueue.writeToControlPV(controlPV);
                } else {
                    if (sys_status != 2) {
                        //储能启动
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(301);
                        controlEMS.setValue(GlobalParam.start);
                        MsgQueue.writeToControlTcp(controlEMS);
                    } else {
                        if (pvm_status != 2) {
                            if (sys_soc <= soc_l) {
                                //储能0功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(0);
                                MsgQueue.writeToControlTcp(controlEMS);
                            } else {
                                int power = pvCtlDto.getGrid_power() + pvCtlDto.getSys_cmd() - pvCtlDto.getAllow_range();
                                //储能功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(power);
                                MsgQueue.writeToControlTcp(controlEMS);
                            }
                        } else {
                            if (sys_soc >= soc_h) {

                                //储能0功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(0);
                                MsgQueue.writeToControlTcp(controlEMS);

                                int pv = pvCtlDto.getGrid_power() - pvCtlDto.getSys_cmd();
                                if (pv < 0) {
                                    controlPV.setSlaveId(1);
                                    controlPV.setAddress(368);
                                    controlPV.setValue(0);
                                    MsgQueue.writeToControlPV(controlPV);
                                } else {
                                    controlPV.setSlaveId(1);
                                    controlPV.setAddress(368);
                                    controlPV.setValue(pv);
                                    MsgQueue.writeToControlPV(controlPV);
                                }


                            } else {
//                                if (pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range() = 0) {
//                                    int power = Math.min(pv_max, pvCtlDto.getAllow_charge());
//                                    //储能功率
//                                    controlEMS.setSlaveId(controlId);
//                                    controlEMS.setAddress(302);
//                                    controlEMS.setValue(-power);
//                                    MsgQueue.writeToControlTcp(controlEMS);
//                                    System.out.println("1");
//                                } else {
                                int power = Math.min(PV_Power + pvCtlDto.getAllow_range(), pvCtlDto.getAllow_charge());
                                //储能功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(-power);
                                MsgQueue.writeToControlTcp(controlEMS);

                                int pv = PV_Power + pvCtlDto.getAllow_range();
                                controlPV.setSlaveId(1);
                                controlPV.setAddress(368);
                                controlPV.setValue(pv);
                                MsgQueue.writeToControlPV(controlPV);
//                                }
                            }
                            //pv功率
//                            pv_ctl(PV_Power,pvCtlDto.getGrid_power(),pvCtlDto.getAllow_range(),pv_max,controlPV);

                        }
                    }
                }
            }
        } else {
            if (pvCtlDto.getPv_power() != pv_max || pvm_status != 2) {
                //光伏下发额定
                controlPV.setSlaveId(1);
                controlPV.setAddress(368);
                controlPV.setValue(pv_max);
                MsgQueue.writeToControlPV(controlPV);
            }
            if (meter_com_status == 1) {
                //储能0功率
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
            } else {
                if (sys_status != 3) {
                    if (sys_status != 2) {
                        //储能启动
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(301);
                        controlEMS.setValue(GlobalParam.start);
                        MsgQueue.writeToControlTcp(controlEMS);
                    } else {
                        if (PV_Power >= 20) {
                            if (sys_soc >= soc_h) {
                                //储能0功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(0);
                                MsgQueue.writeToControlTcp(controlEMS);
                            } else {
                                int power = Math.min(PV_Power, pvCtlDto.getAllow_charge());
                                //储能功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(-power);
                                MsgQueue.writeToControlTcp(controlEMS);
                            }
                        } else {
                            if (sys_soc <= soc_l) {
                                //储能0功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(0);
                                MsgQueue.writeToControlTcp(controlEMS);
                            } else {
                                int power = pvCtlDto.getGrid_power() + pvCtlDto.getSys_cmd() - pvCtlDto.getAllow_range();
                                //储能功率
                                controlEMS.setSlaveId(controlId);
                                controlEMS.setAddress(302);
                                controlEMS.setValue(power);
                                MsgQueue.writeToControlTcp(controlEMS);
                            }
                        }
                    }
                }
            }
        }

    }

    /*
     * 并网策略--AHLS-02专用
     * */
    public static void on_ToGrid_Time(ControlDto controlEMS, ControlDto controlPV) {

        ParamDto param = SQLiteParamUtils.query();
        int soc_h = Integer.parseInt(param.getSoc_h());
        int soc_l = Integer.parseInt(param.getSoc_l());
        int pv_max = param.getPv_max() * 10;
        int allowance = param.getGrid_alw() * 10;
        int stepSize = param.getStep_size() * 10;
        int antiReflux = param.getAnti_reflux();
        PvCtlDto pvCtlDto = new PvCtlDto();
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short pvC_status = Communication.pvm_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        short UC_status = (short) GlobalParam.com1Fault;//uc通讯状态 0 表示正常 1 表示中断

        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[63];//PHC系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        short PV_Power = OverallEMSData[14003];//光伏实际输出功率
        short PV_ctl_power = OverallEMSData[368];//光伏控制功率
        short PV_Max_Power = (short) pv_max;//光伏额定输出功率
        short pvm_status = OverallEMSData[14000];//光伏逆变器的状态

        //电网功率
        short Grid_meter_power = ammeterUtils.getGrid_Power();
        //test.info("电网功率:" + Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1 + "光伏功率" + PV_Power * 0.1);
        //允许充电
        pvCtlDto.setAllow_charge(OverallEMSData[5]);
        //允许放电
        pvCtlDto.setAllow_discharge(OverallEMSData[4]);
        //电网功率
        pvCtlDto.setGrid_power(Grid_meter_power);
        //高压保护
        pvCtlDto.setH_alarm(OverallEMSData[16]);
        //低压保护
        pvCtlDto.setL_alarm(OverallEMSData[17]);
        //光伏现在功率
        pvCtlDto.setPv_power(OverallEMSData[368]);
        //光伏额定功率
        pvCtlDto.setPv_rated_power(pv_max);
        //系统soc
        pvCtlDto.setSys_soc(OverallEMSData[26]);
        //系统指令功率
        pvCtlDto.setSys_cmd(OverallEMSData[63]);
        //容差
        pvCtlDto.setAllow_range(allowance);
        //soc_h
        pvCtlDto.setSoc_h(soc_h);
        //soc_l
        pvCtlDto.setSoc_l(soc_l);
        //步长
        pvCtlDto.setStep_size(stepSize);
        //防逆流设置
        pvCtlDto.setAntiReflux(antiReflux);
        //判断电表和光伏通讯是否中断
        if (meter_com_status == 0 && pvC_status == 0) {
            //test.info("电表与光伏通讯正常");
            //判断系统状态
            if ((sys_status == 2 && sys_ctl_status == 6)) {
                if (pvCtlDto.getAntiReflux() == 0) {
                    sys_ctl_pv_normal_noReflux(pvCtlDto, controlEMS, controlPV);
                } else {
                    if (pvm_status == 2) {
                        //test.info("光伏正常");
                        sys_ctl_pv_normal(pvCtlDto, controlEMS, controlPV);
                    } else {//光伏故障
                        //test.info("光伏故障");
                        sys_ctl_pv_abnormal(pvCtlDto, controlEMS);
                    }
                }
            } else if (sys_status == 1 && sys_ctl_status == 2) {
                //test.info("系统停机");
                //判断电网功率不为0
                if (Grid_meter_power != 0) {
                    //给系统下发启动
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(GlobalParam.onoff);
                    controlEMS.setValue(GlobalParam.start);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
            } else if (sys_status == 3 || UC_status == 1) { //系统故障
                if (pvm_status == 2) {
                    //执行光伏的策略
                    //test.info("系统故障或者uc通讯中断，光伏正常");
                    if (pvCtlDto.getAntiReflux() == 1) {
                        pv_ctl_essFault(PV_Power, Grid_meter_power, allowance, PV_Max_Power, controlPV);
                    } else {
                        //设置光伏功率为最大功率
                        controlPV.setSlaveId(1);
                        controlPV.setAddress(368);
                        controlPV.setValue(pv_max);
                        MsgQueue.writeToControlPV(controlPV);
                    }
                }
            }
            //通讯中断下发停机
        } else {
            //test.info("通讯中断");
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(301);
            controlEMS.setValue(GlobalParam.stop);
            MsgQueue.writeToControlTcp(controlEMS);

            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(0);
            MsgQueue.writeToControlPV(controlPV);

        }
    }


    public static void on_ToGrid_Time_noPV(ControlDto controlEMS) {
        PvCtlDto pvCtlDto = new PvCtlDto();
        ParamDto param = SQLiteParamUtils.query();
        int soc_h = Integer.parseInt(param.getSoc_h());
        int soc_l = Integer.parseInt(param.getSoc_l());
        int pv_max = param.getPv_max() * 10;
        int stepSize = param.getStep_size() * 10;
        int allowance = param.getGrid_alw() * 10;
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[63];//PHC系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        //电网功率
        short Grid_meter_power = ammeterUtils.getGrid_Power();
        //允许充电
        pvCtlDto.setAllow_charge(OverallEMSData[5]);
        //允许放电
        pvCtlDto.setAllow_discharge(OverallEMSData[4]);
        //电网功率
        pvCtlDto.setGrid_power(Grid_meter_power);
//        //高压保护
        pvCtlDto.setH_alarm(OverallEMSData[16]);
//        //低压保护
        pvCtlDto.setL_alarm(OverallEMSData[17]);
        //光伏功率
        pvCtlDto.setPv_power(OverallEMSData[368]);
        //光伏额定功率
        pvCtlDto.setPv_rated_power(pv_max);
        //系统soc
        pvCtlDto.setSys_soc(OverallEMSData[26]);
//        pvCtlDto.setSys_soc(30);
        //系统指令功率
        pvCtlDto.setSys_cmd(OverallEMSData[63]);
        //容差
        pvCtlDto.setAllow_range(allowance);
        //soc_h
        pvCtlDto.setSoc_h(soc_h);
        //soc_l
        pvCtlDto.setSoc_l(soc_l);
        //步长
        pvCtlDto.setStep_size(stepSize);

        //判断电表和光伏通讯是否中断
        if (meter_com_status == 0) {
            //判断系统状态
            if ((sys_status == 2 && sys_ctl_status == 6)) {
                sys_ctl_time_nopv(pvCtlDto, controlEMS);
            } else if (sys_status == 1 && sys_ctl_status == 2) {
                //判断电网功率不为0
                if (Grid_meter_power != 0) {
                    //给系统下发启动
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(GlobalParam.onoff);
                    controlEMS.setValue(GlobalParam.start);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
            } else if ((sys_status == 1 && sys_ctl_status == 6)) {
                //防止系统0功率,系统没启动，运行充放电功率为0，系统进入不了调频
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(20);
                MsgQueue.writeToControlTcp(controlEMS);
            }
            //通讯中断下发停机
        } else {
            controlEMS.setSlaveId(16);
            controlEMS.setAddress(301);
            controlEMS.setValue(GlobalParam.stop);
            MsgQueue.writeToControlTcp(controlEMS);
        }
    }

    public static void on_ToGrid_MPPT(ControlDto controlEMS, ControlDto controlPV) {
        ParamDto param = SQLiteParamUtils.query();
        int soc_h = Integer.parseInt(param.getSoc_h());
        int soc_l = Integer.parseInt(param.getSoc_l());
        int pv_max = param.getPv_max() * 10;
        int allowance = param.getGrid_alw() * 10;
        int stepSize = param.getStep_size() * 10;
        PvCtlDto pvCtlDto = new PvCtlDto();
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short UC_status = (short) GlobalParam.com1Fault;//uc通讯状态 0 表示正常 1 表示中断

        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[63];//PHC系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        short PV_Power = OverallEMSData[14003];//光伏实际输出功率

        //电网功率
        short Grid_meter_power = ammeterUtils.getGrid_Power();
        //test.info("电网功率:" + Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1 + "光伏功率" + PV_Power * 0.1);
//        System.out.println("电网功率:" + Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1 + "光伏功率:" + PV_Power * 0.1 + "光伏限制功率：" + DataArray.data[368] * 0.1 + "kw," + "socl:" + soc_l + "soc:" + DataArray.data[26] + "soch:" + soc_h);

        //允许充电
        pvCtlDto.setAllow_charge(OverallEMSData[5]);
        //允许放电
        pvCtlDto.setAllow_discharge(OverallEMSData[4]);
        //电网功率
        pvCtlDto.setGrid_power(Grid_meter_power);
        //高压保护
        pvCtlDto.setH_alarm(OverallEMSData[16]);
        //低压保护
        pvCtlDto.setL_alarm(OverallEMSData[17]);
        //光伏现在功率
        pvCtlDto.setPv_power(OverallEMSData[368]);
        //光伏额定功率
        pvCtlDto.setPv_rated_power(pv_max);
        //系统soc
        pvCtlDto.setSys_soc(OverallEMSData[26]);
        //系统指令功率
        pvCtlDto.setSys_cmd(OverallEMSData[63]);

        //容差
        pvCtlDto.setAllow_range(allowance);
        //soc_h
        pvCtlDto.setSoc_h(soc_h);
        //soc_l
        pvCtlDto.setSoc_l(soc_l);
        //步长
        pvCtlDto.setStep_size(stepSize);

        //判断电表和光伏通讯是否中断
        if (meter_com_status == 0) {
            //判断系统状态
            if ((sys_status == 2 && sys_ctl_status == 6)) {
                sys_ctl_pv_abnormal_MPPT(pvCtlDto, controlEMS);
            } else if (sys_status == 1 && sys_ctl_status == 2) {
                //test.info("系统停机");
                //判断电网功率不为0
                if (Grid_meter_power != 0) {
                    //给系统下发启动
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(GlobalParam.onoff);
                    controlEMS.setValue(GlobalParam.start);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
            }
            //通讯中断下发停机
        } else {
            //test.info("通讯中断");
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(301);
            controlEMS.setValue(GlobalParam.stop);
            MsgQueue.writeToControlTcp(controlEMS);
        }
    }

    /**
     * 光伏自发自用
     */

    public static void on_ToGrid(ControlDto controlEMS, ControlDto controlPV) {
        Logger test = LoggerFactory.getLogger("HTTPServer");
        ParamDto param = SQLiteParamUtils.query();
        int soc_h = Integer.parseInt(param.getSoc_h());
        int soc_l = Integer.parseInt(param.getSoc_l());
        int pv_max = param.getPv_max() * 10;
        int allowance = param.getGrid_alw() * 10;
        int stepSize = param.getStep_size() * 10;
        int antiReflux = param.getAnti_reflux();
        PvCtlDto pvCtlDto = new PvCtlDto();
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short pvC_status = Communication.pvm_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[63];//PHC系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        short PV_Power = OverallEMSData[14003];//光伏实际输出功率
        short PV_ctl_power = OverallEMSData[368];//光伏控制功率
        short PV_Max_Power = (short) pv_max;//光伏额定输出功率
        short pvm_status = OverallEMSData[14000];//光伏逆变器的状态

        //电网功率
        short Grid_meter_power = ammeterUtils.getGrid_Power();
        test.info("电网功率:" + Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1 + "光伏功率:" + PV_Power * 0.1);
        System.out.println("电网功率:" + Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1 + "光伏功率:" + PV_Power * 0.1 + "socl:" + soc_l + "soc:" + OverallEMSData[26] + "soch:" + soc_h);
        //允许充电
        pvCtlDto.setAllow_charge(OverallEMSData[5]);
        //允许放电
        pvCtlDto.setAllow_discharge(OverallEMSData[4]);
        //电网功率
        pvCtlDto.setGrid_power(Grid_meter_power);
        //高压保护
        pvCtlDto.setH_alarm(OverallEMSData[16]);
        //低压保护
        pvCtlDto.setL_alarm(OverallEMSData[17]);
        //光伏现在功率
        pvCtlDto.setPv_power(OverallEMSData[368]);
        //光伏额定功率
        pvCtlDto.setPv_rated_power(pv_max);
        //系统soc
        pvCtlDto.setSys_soc(OverallEMSData[26]);
        //系统指令功率
        pvCtlDto.setSys_cmd(OverallEMSData[63]);

        //容差
        pvCtlDto.setAllow_range(allowance);
        //soc_h
        pvCtlDto.setSoc_h(soc_h);
        //soc_l
        pvCtlDto.setSoc_l(soc_l);
        //步长
        pvCtlDto.setStep_size(stepSize);
        //防逆流设置
        pvCtlDto.setAntiReflux(antiReflux);

        //判断电表和光伏通讯是否中断
        if (meter_com_status == 0 && pvC_status == 0) {

            //判断系统状态
            if ((sys_status == 2 && sys_ctl_status == 6)) {
                if (pvm_status == 2) {
                    sys_ctl_notime(pvCtlDto, controlEMS, controlPV);
                } else {
                    sys_ctl_notime_nopv(pvCtlDto, controlEMS);
                }
            } else if (sys_status == 1 && sys_ctl_status == 2) {
                //判断电网功率不为0
                if (Grid_meter_power != 0) {
                    //给系统下发启动
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(GlobalParam.onoff);
                    controlEMS.setValue(GlobalParam.start);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
            } else if (sys_status == 3) { //系统故障
                if (pvm_status == 2) {
                    //执行光伏的策略
                    if (antiReflux == 1) {//防逆流
                        pv_ctl_essFault(PV_Power, Grid_meter_power, allowance, PV_Max_Power, controlPV);
                    } else {//不防逆流
                        //设置光伏功率为最大功率
                        controlPV.setSlaveId(1);
                        controlPV.setAddress(368);
                        controlPV.setValue(pv_max);
                        MsgQueue.writeToControlPV(controlPV);
                    }
                }
            } else if ((sys_status == 1 && sys_ctl_status == 6)) {
                //防止系统0功率,系统没启动，运行充放电功率为0，系统进入不了调频
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(20);
                MsgQueue.writeToControlTcp(controlEMS);
            }
            //通讯中断下发停机
        } else {
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(301);
            controlEMS.setValue(GlobalParam.stop);
            MsgQueue.writeToControlTcp(controlEMS);

            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(0);
            MsgQueue.writeToControlPV(controlPV);

        }
    }

    /*
     * 光伏控制逻辑
     * */
    public static void pv_ctl(int pvCtl_power, int grid_power, int allow_range, int pv_max, ControlDto controlPV) {
        int power = pvCtl_power + grid_power - allow_range;
        if (power < 0) {
            //设置光伏功率为0
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(0);
            MsgQueue.writeToControlPV(controlPV);
        } else if (power >= pv_max) {
            if (pvCtl_power != pv_max) {
                //设置光伏功率为最大功率
                controlPV.setSlaveId(1);
                controlPV.setAddress(368);
                controlPV.setValue(pv_max);
                MsgQueue.writeToControlPV(controlPV);
            }
        } else {
//            if (power != pvCtl_power) {
            //设置光伏功率
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(power);
            MsgQueue.writeToControlPV(controlPV);
//            }
        }

    }

    public static void sys_ctl_time_nopv(PvCtlDto pvCtlDto, ControlDto controlEMS) {
        //计算出系统功率变化值
        int power;
        //时段校验算出功率
        AtomicInteger AtoPower_ESS = PV_Time_ctl(pvCtlDto);
        int Power_Time = AtoPower_ESS.intValue();
        //电网逆流与用电
        int P_Grid = pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();

        if ((pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) && Power_Time > 0) {
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(302);
            controlEMS.setValue(0);
            MsgQueue.writeToControlTcp(controlEMS);
        } else if ((pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) && Power_Time < 0) {
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(302);
            controlEMS.setValue(0);
            MsgQueue.writeToControlTcp(controlEMS);
        } else {
            if (P_Grid < 0) {
                int step = pvCtlDto.getStep_size();
                if (pvCtlDto.getGrid_power() > 0) {
                    step = Math.min(Math.abs(pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range()), step);
                } else {
                    step = Math.min(Math.abs(Math.abs(pvCtlDto.getGrid_power()) + pvCtlDto.getAllow_range()), step);
                }
                power = pvCtlDto.getSys_cmd() - step;

                int CMD_Power = power * Power_Time;
                if (CMD_Power > 0) {
                    power = Math.min(Math.abs(power), Math.abs(Power_Time));
                    power = Power_Time > 0 ? power : -power;
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(power);
                    MsgQueue.writeToControlTcp(controlEMS);
                } else {
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(0);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
            } else if (P_Grid > 0) {
                int step = pvCtlDto.getStep_size();
                step = Math.min(Math.abs(pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range()), step);

                power = pvCtlDto.getSys_cmd() + step;
                int CMD_Power = power * Power_Time;
                if (CMD_Power > 0) {
                    power = Math.min(Math.abs(power), Math.abs(Power_Time));
                    power = Power_Time > 0 ? power : -power;
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(power);
                    MsgQueue.writeToControlTcp(controlEMS);
                } else {
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(0);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
            }
        }
    }

    /**
     * pv下发功率对比
     */
    public static void pv_power(int power, int pv_max, ControlDto controlPV) {
        if (power < 0) {
            //设置光伏功率为0
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(0);
            MsgQueue.writeToControlPV(controlPV);
        } else if (power >= pv_max) {
            //设置光伏功率为最大功率
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(pv_max);
            MsgQueue.writeToControlPV(controlPV);
        } else {
            //设置光伏功率
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(power);
            MsgQueue.writeToControlPV(controlPV);

        }
    }

    /**
     * 系统故障，光伏控制策略，使用实际的光伏功率进行调节（14003）
     */
    public static void pv_ctl_essFault(int pvCtl_power, int grid_power, int allow_range, int pv_max, ControlDto controlPV) {
        int power = pvCtl_power + grid_power - allow_range;
        if (power < 0) {
            //设置光伏功率为0
            System.out.println("3");
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(0);
            MsgQueue.writeToControlPV(controlPV);
        } else if (power >= pv_max) {
            if (pvCtl_power != pv_max) {
                //设置光伏功率为最大功率
                controlPV.setSlaveId(1);
                controlPV.setAddress(368);
                controlPV.setValue(pv_max);
                MsgQueue.writeToControlPV(controlPV);
            }
        } else {
            if (power != pvCtl_power) {
                //设置光伏功率
                controlPV.setSlaveId(1);
                controlPV.setAddress(368);
                controlPV.setValue(power);
                MsgQueue.writeToControlPV(controlPV);
            }
        }

    }

    /*
     * 离网控制策略-AHLS-02专用
     * */
    public void off_ToGrid() {
        ParamDto param = SQLiteParamUtils.query();
        int soc_h = Integer.parseInt(param.getSoc_h());
        int soc_l = Integer.parseInt(param.getSoc_l());
        int pv_max = param.getPv_max();
        int allow_range = param.getGrid_alw();
        ControlDto controlTcp = new ControlDto();
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short pvC_status = Communication.pvm_interrupt;//pvm通讯状态 0 表示正常 1 表示中断
        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[6];//系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        short PV_Power = OverallEMSData[14003];//光伏输出功率
        short PV_ctl_power = OverallEMSData[368];//光伏控制功率
        short PV_Max_Power = OverallEMSData[14004];//光伏额定输出功率
        short L1_High2 = DataArray2.data[112];
        short L1_low2 = DataArray2.data[113];
        short L2_High2 = DataArray2.data[114];
        short L2_low2 = DataArray2.data[115];
        short L3_High2 = DataArray2.data[116];
        short L3_low2 = DataArray2.data[117];
        //负载功率
        short Load_meter_power = (short) HexUtils.Power(L1_High2, L1_low2, L2_High2, L2_low2, L3_High2, L3_low2);
    /*    System.out.println("调节时间" + System.currentTimeMillis() + "负载功率" +
                Load_meter_power * 0.1 //+ "----负载功率" //+ Load_meter_power * 0.1
                + "系统功率" + sys_power * 0.1);*/
        //先判断系统状态（系统停机，电表没有供电）
        if ((sys_status != 1 && sys_ctl_status == 6)) {
            //判断电表和光伏控制器通讯是否中断
            if (meter_com_status == 0 && pvC_status == 0) {
                //判断系统状态
                if (sys_status == 2) {
                    //判断光伏是否大于负载
                    if (PV_Power > Load_meter_power) {
                        //通过系统的SOC来判断，提前进行控制
                        //if (sys_soc > soc_h || no_charge_mark == 1) {
                        if (sys_soc > soc_h) {
                            if (PV_ctl_power != (int) Load_meter_power - allow_range) {
                                //限制光伏的输出功率=负载
                                controlTcp.setSlaveId(1);
                                controlTcp.setAddress(368);
                                controlTcp.setValue((int) Load_meter_power - allow_range);
                                MsgQueue.writeToControlPV(controlTcp);
                            }
                        }
                    } else if (PV_Power < Load_meter_power - allow_range) {
                        //判断光伏是否为额定输出
                        if (PV_ctl_power != PV_Max_Power) {
                            //设置光伏的输出功率
                            controlTcp.setSlaveId(1);
                            controlTcp.setAddress(368);
                            controlTcp.setValue((int) PV_Max_Power);
                            MsgQueue.writeToControlPV(controlTcp);
                        }
                        if (sys_soc < soc_l) {
                            //控制系统停机
                            controlTcp.setSlaveId(controlId);
                            controlTcp.setAddress(301);
                            controlTcp.setValue(GlobalParam.stop);
                            MsgQueue.writeToControlTcp(controlTcp);
                        }
                    }
                } else if (sys_status == 3) { //系统故障
                    //判断光伏和负载之间的关系
                    if (PV_Power > Load_meter_power) {
                        if (PV_ctl_power != Load_meter_power) {
                            //限制光伏的输出功率
                            controlTcp.setSlaveId(1);
                            controlTcp.setAddress(368);
                            controlTcp.setValue((int) Load_meter_power);
                            MsgQueue.writeToControlPV(controlTcp);
                        }
                    } else if (PV_Power < Load_meter_power) {
                        if (PV_ctl_power != PV_Max_Power) {
                            //将光伏输出功率改为额定功率
                            controlTcp.setSlaveId(1);
                            controlTcp.setAddress(368);
                            controlTcp.setValue((int) Load_meter_power);
                            MsgQueue.writeToControlPV(controlTcp);
                        }
                    }
                }
            }
        } else if (sys_status == 1 && sys_ctl_status == 2) {
            //给系统下发启动
            if (sys_ctl_status != GlobalParam.start) {
                controlTcp.setSlaveId(controlId);
                controlTcp.setAddress(GlobalParam.onoff);
                controlTcp.setValue(GlobalParam.start);
                MsgQueue.writeToControlTcp(controlTcp);
            }
        }
    }

    //时段功率校验
    public static AtomicInteger PV_Time_ctl(PvCtlDto pvCtlDto) {
        AtomicInteger Powe_ESS = new AtomicInteger();
        //获得所有设置的时段
        List<PVTimeDto> query = SQLPVTimeUtils.query();
        if (query.size() > 0) {
            GlobalParam.a = 0;
            for (int i = 0; i < query.size(); i++) {
                PVTimeDto item = query.get(i);
                String tiemNow = DateUtils.tiemHour();
                String startTime = item.getStartTime();
                String endTime = item.getEndTime();
                boolean result = DateUtils.belongCalendar(tiemNow, startTime, endTime);
                int Power = item.getPower();
                int timePower = Power * 10;
                int allowCharge = pvCtlDto.getAllow_charge();
                int allowDischarge = pvCtlDto.getAllow_discharge();
                if (result == true) {
                    if (timePower < 0) {
                        int min = Math.min(Math.abs(timePower), Math.abs(allowCharge));
                        Powe_ESS.set(-min);
                    } else if (timePower == 0) {
                        Powe_ESS.set(0);
                    } else if (timePower > 0) {
                        int min = Math.min(Math.abs(timePower), Math.abs(allowDischarge));
                        Powe_ESS.set(min);
                    }
                    GlobalParam.a = 0;
                    break;
                } else {
                    GlobalParam.a++;
                    if (GlobalParam.a == query.size()) {
                        Powe_ESS.set(0);
                    }
                }
            }
        }
        return Powe_ESS;
    }

    /**
     * 储能正常，光伏不正常不带时段
     */

    public static void sys_ctl_notime_nopv(PvCtlDto pvCtlDto, ControlDto controlEMS) {
        //计算出系统功率变化值
        int power = pvCtlDto.getSys_cmd() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();

        if (power < 0) {
            if (pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) {
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
                //给储能功率赋值
                OverallEMSData[63] = 0;
            } else {
                if (Math.abs(power) <= pvCtlDto.getAllow_charge()) {
                    if (power != pvCtlDto.getSys_cmd()) {
                        int i = ArrayUtils.NormalNum(power);
                        int minPower = ArrayUtils.MinPower(power, i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                } else {
                    if (pvCtlDto.getAllow_charge() != Math.abs(pvCtlDto.getSys_cmd())) {
                        //设置储能的功率
                        int i = ArrayUtils.NormalNum(-pvCtlDto.getAllow_charge());
                        int minPower = ArrayUtils.MinPower(-pvCtlDto.getAllow_charge(), i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                }
            }
        } else if (power > 0) {
            if (pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) {
                //降储能功率为0
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
                //给储能功率赋值
                OverallEMSData[63] = 0;
            } else {
                if (power >= pvCtlDto.getAllow_discharge()) {
                    //提高储能功率
                    int i = ArrayUtils.NormalNum(pvCtlDto.getAllow_discharge());
                    int minPower = ArrayUtils.MinPower(pvCtlDto.getAllow_discharge(), i);
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(minPower);
                    MsgQueue.writeToControlTcp(controlEMS);
                    //设置储能功率
                    OverallEMSData[63] = (short) pvCtlDto.getAllow_discharge();
                } else {
                    if (power != pvCtlDto.getSys_cmd()) {
                        int i = ArrayUtils.NormalNum(power);
                        int minPower = ArrayUtils.MinPower(power, i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                }
            }
        }
    }

    /**
     * 系统正常，光伏不正常
     */

    public static void sys_ctl_pv_abnormal(PvCtlDto pvCtlDto, ControlDto controlEMS) {
        //计算出系统功率变化值
        int power;
        //时段校验算出功率
        AtomicInteger AtoPower_ESS = PV_Time_ctl(pvCtlDto);
        int Power_Time = AtoPower_ESS.intValue();
        //电网逆流与用电
        int P_Grid = pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();

        if ((pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) && Power_Time > 0) {
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(302);
            controlEMS.setValue(0);
            MsgQueue.writeToControlTcp(controlEMS);
        } else if ((pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) && Power_Time < 0) {
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(302);
            controlEMS.setValue(0);
            MsgQueue.writeToControlTcp(controlEMS);
        } else {
            int step = pvCtlDto.getStep_size();
            step = Math.min(Math.abs(pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range()), step);
            if (P_Grid < 0) {
                power = pvCtlDto.getSys_cmd() - step;
            } else {
                power = pvCtlDto.getSys_cmd() + step;
            }
            int CMD_Power = power * Power_Time;
            if (CMD_Power > 0) {
                power = Math.min(Math.abs(power), Math.abs(Power_Time));
                power = Power_Time > 0 ? power : -power;
                int i = ArrayUtils.NormalNum(power);
                int minPower = ArrayUtils.MinPower(power, i);
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(minPower);
                MsgQueue.writeToControlTcp(controlEMS);
            } else {
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
            }
        }
    }


    public static void sys_ctl_pv_abnormal_MPPT(PvCtlDto pvCtlDto, ControlDto controlEMS) {
        ControlDto controlTcp = new ControlDto();
        //计算出系统功率变化值
        int power = pvCtlDto.getSys_cmd() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();
        if (power < 0) {
            if (pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) {
                controlTcp.setSlaveId(controlId);
                controlTcp.setAddress(302);
                controlTcp.setValue(0);
                MsgQueue.writeToControlTcp(controlTcp);
            } else {
                controlTcp.setSlaveId(controlId);
                controlTcp.setAddress(302);
                controlTcp.setValue(power);
                MsgQueue.writeToControlTcp(controlTcp);
            }
        } else if (power > 0) {
            if (pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) {
                //降储能功率为0
                controlTcp.setSlaveId(controlId);
                controlTcp.setAddress(302);
                controlTcp.setValue(0);
                MsgQueue.writeToControlTcp(controlTcp);
            } else {
                controlTcp.setSlaveId(controlId);
                controlTcp.setAddress(302);
                controlTcp.setValue(power);
                MsgQueue.writeToControlTcp(controlTcp);
            }
        }
    }

    /*
     *系统控制策略不加时段
     * */
    public static void sys_ctl_notime(PvCtlDto pvCtlDto, ControlDto controlEMS, ControlDto controlPV) {
        //计算出系统功率变化值
        int power = pvCtlDto.getSys_cmd() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();
        if (pvCtlDto.getAntiReflux() == 0) {
            if (pvCtlDto.getPv_power() != pvCtlDto.getPv_rated_power()) {
                controlPV.setSlaveId(1);
                controlPV.setAddress(368);
                controlPV.setValue(pvCtlDto.getPv_rated_power());
                MsgQueue.writeToControlPV(controlPV);
            }
        }
        if (power < 0) {
            if (pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) {
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
                //给储能功率赋值
                OverallEMSData[63] = 0;
                //执行光伏的限制策略
                //获得光伏相关的参数
                //1.光伏控制功率
                short pc_ctl_p = OverallEMSData[368];
                //2.电网功率
                short Grid_meter_power = HexUtils.Power(DataArray2.data[12], DataArray2.data[13], DataArray2.data[14], DataArray2.data[15], DataArray2.data[16], DataArray2.data[17]);
                //3.容差
                int allow_range = pvCtlDto.getAllow_range();
                //4.光伏额定功率
                short pv_Max = (short) pvCtlDto.getPv_rated_power();
                if (pvCtlDto.getAntiReflux() == 1) {//防逆流
                    pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);
                }
            } else {
                if (Math.abs(power) <= pvCtlDto.getAllow_charge()) {
                    if (power != pvCtlDto.getSys_cmd()) {
                        int i = ArrayUtils.NormalNum(power);
                        int minPower = ArrayUtils.MinPower(power, i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                    //1.光伏控制功率
                    short pc_ctl_p = OverallEMSData[368];
                    //2.电网功率
                    short Grid_meter_power = (short) HexUtils.Power(DataArray2.data[12], DataArray2.data[13], DataArray2.data[14], DataArray2.data[15], DataArray2.data[16], DataArray2.data[17]);
                    //3.容差
                    int allow_range = pvCtlDto.getAllow_range();
                    //4.光伏额定功率
//                short pv_Max = DataArray.data[14004];
                    short pv_Max = (short) pvCtlDto.getPv_rated_power();
                    pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);
                } else {
                    //设置储能的功率
                    int i = ArrayUtils.NormalNum(-pvCtlDto.getAllow_charge());
                    int minPower = ArrayUtils.MinPower(-pvCtlDto.getAllow_charge(), i);
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(minPower);
                    MsgQueue.writeToControlTcp(controlEMS);
                    if (pvCtlDto.getAntiReflux() == 1) {//防逆流
                        //限制光伏的功率
                        int pv_power = pvCtlDto.getPv_power() + power + pvCtlDto.getAllow_charge();
                        if (pv_power < 0) {
                            controlPV.setSlaveId(1);
                            controlPV.setAddress(368);
                            controlPV.setValue(0);
                            MsgQueue.writeToControlPV(controlPV);
                        } else if (pv_power > pvCtlDto.getPv_rated_power()) {
                            controlPV.setSlaveId(1);
                            controlPV.setAddress(368);
                            controlPV.setValue(pvCtlDto.getPv_rated_power());
                            MsgQueue.writeToControlPV(controlPV);
                        } else {
                            controlPV.setSlaveId(1);
                            controlPV.setAddress(368);
                            controlPV.setValue(pv_power);
                            MsgQueue.writeToControlPV(controlPV);
                        }
                    }
                }
            }
        } else if (power > 0) {
            //1.光伏控制功率
            short pc_ctl_p = OverallEMSData[368];
            //2.电网功率
            short Grid_meter_power = (short) HexUtils.Power(DataArray2.data[12], DataArray2.data[13], DataArray2.data[14], DataArray2.data[15], DataArray2.data[16], DataArray2.data[17]);
            //3.容差
            int allow_range = pvCtlDto.getAllow_range();
            //4.光伏额定功率
//                short pv_Max = DataArray.data[14004];
            short pv_Max = (short) pvCtlDto.getPv_rated_power();
            if (pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) {
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
                //给储能功率赋值
                OverallEMSData[63] = 0;
                //执行光伏的限制策略
                if (pvCtlDto.getAntiReflux() == 1) {//防逆流
                    pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);
                }
            } else {
                if (pvCtlDto.getPv_power() != pvCtlDto.getPv_rated_power()) {
                    //针对系统无法充电时，防止大范围调节光伏导致电网出现大功率逆流
                    if (pvCtlDto.getSys_soc() > pvCtlDto.getSoc_h()) {
                        if (pvCtlDto.getAntiReflux() == 1) {//防逆流
                            pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);
                        }
                    } else {
                        //调节光伏功率
                        controlPV.setSlaveId(1);
                        controlPV.setAddress(368);
                        controlPV.setValue(pvCtlDto.getPv_rated_power());
                        MsgQueue.writeToControlPV(controlPV);
                    }
                } else {
                    if (power >= pvCtlDto.getAllow_discharge()) {
                        //提高储能功率
                        int i = ArrayUtils.NormalNum(pvCtlDto.getAllow_discharge());
                        int minPower = ArrayUtils.MinPower(pvCtlDto.getAllow_discharge(), i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                        //设置储能功率
                        OverallEMSData[63] = (short) pvCtlDto.getAllow_discharge();
                    } else {
                        int i = ArrayUtils.NormalNum(power);
                        int minPower = ArrayUtils.MinPower(power, i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                }
            }
        }
    }

    /*
     *系统控制策略,光伏正常
     * */
    public static void sys_ctl_pv_normal(PvCtlDto pvCtlDto, ControlDto controlEMS, ControlDto controlPV) {
        int P_Grid = pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();
        int power;
        //1.光伏控制功率
        short pc_ctl_p = OverallEMSData[368];
        //2.电网功率
        short Grid_meter_power = HexUtils.Power(DataArray2.data[12], DataArray2.data[13], DataArray2.data[14], DataArray2.data[15], DataArray2.data[16], DataArray2.data[17]);
        //3.容差
        int allow_range = pvCtlDto.getAllow_range();
        //4.光伏额定功率
        short pv_Max = (short) pvCtlDto.getPv_rated_power();
        //时段校验算出功率
        AtomicInteger AtoPower_ESS = PV_Time_ctl(pvCtlDto);
        int Power_Time = AtoPower_ESS.intValue();
        if ((pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) && Power_Time > 0) {
            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(302);
            controlEMS.setValue(0);
            MsgQueue.writeToControlTcp(controlEMS);

            //光伏限制策略
            pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);
        } else if ((pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) && Power_Time < 0) {
            //先控制光伏
            int p1 = pvCtlDto.getPv_power() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range() - Math.abs(pvCtlDto.getSys_cmd());
            int min_power = Math.min(p1, pv_Max);
            pv_power(min_power, pv_Max, controlPV);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            controlEMS.setSlaveId(controlId);
            controlEMS.setAddress(302);
            controlEMS.setValue(0);
            MsgQueue.writeToControlTcp(controlEMS);


        } else {
            if (P_Grid < 0) {
                if (pvCtlDto.getSys_cmd() == Power_Time && pvCtlDto.getSys_cmd() < 0) {//储能已经尽最大能力进行调节
                    //控制光伏策略
                    pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);

                } else {//WR02电网逆流策略
                    int step = Math.min(Math.abs(pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range()), pvCtlDto.getStep_size());
                    power = pvCtlDto.getSys_cmd() - step;
                    int CMD_Power = power * Power_Time;
                    if (CMD_Power > 0) {
                        power = Math.min(Math.abs(power), Math.abs(Power_Time));
                        power = Power_Time > 0 ? power : -power;
                        int i = ArrayUtils.NormalNum(power);
                        int minPower = ArrayUtils.MinPower(power, i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    } else {
                        if (pvCtlDto.getSys_cmd() < 0) {
                            int pv = OverallEMSData[14003] - Math.abs(pvCtlDto.getSys_cmd());
//                            int min = Math.min(0, pv);
                            //限制光伏的输出功率P =min(0, |P光伏实际功率- Pcmd|)
                            pv_power(pv, pv_Max, controlPV);
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(0);
                        MsgQueue.writeToControlTcp(controlEMS);

                        //控制光伏策略
                        pv_ctl(pc_ctl_p, Grid_meter_power, allow_range, pv_Max, controlPV);
                    }
                }
            } else if (P_Grid > 0) {
                if (pvCtlDto.getPv_power() == pvCtlDto.getPv_rated_power()) {
                    //WR02电网放电储能控制策略
                    int step = pvCtlDto.getStep_size();
                    step = Math.min(Math.abs(pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range()), step);

                    power = pvCtlDto.getSys_cmd() + step;
                    int CMD_Power = power * Power_Time;
                    if (CMD_Power > 0) {
                        power = Math.min(Math.abs(power), Math.abs(Power_Time));
                        power = Power_Time > 0 ? power : -power;
                        int i = ArrayUtils.NormalNum(power);
                        int minPower = ArrayUtils.MinPower(power, i);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(minPower);
                        MsgQueue.writeToControlTcp(controlEMS);
                    } else {
                        if (pvCtlDto.getSys_cmd() < 0) {
                            int pv = OverallEMSData[14003] - Math.abs(pvCtlDto.getSys_cmd());
//                            int min = Math.min(0, pv);
                            //限制光伏的输出功率P =P光伏实际功率- |Pcmd|
                            pv_power(pv, pv_Max, controlPV);
                        }
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(0);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                } else {
                    //光伏不等于额定功率
                    int judgment_power = pvCtlDto.getSys_cmd() * Power_Time;
                    int p1;
                    if (judgment_power <= 0) {
                        if (pvCtlDto.getSys_cmd() < 0) {
                            p1 = pvCtlDto.getPv_power() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range() - Math.abs(pvCtlDto.getSys_cmd());
                        } else {
                            p1 = pvCtlDto.getPv_power() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();
                        }
                        int min_power = Math.min(p1, pv_Max);
                        pv_power(min_power, pv_Max, controlPV);
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }

                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(0);
                        MsgQueue.writeToControlTcp(controlEMS);
                    } else {
                        //下发p1
                        p1 = pvCtlDto.getPv_power() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();
                        pv_power(p1, pv_Max, controlPV);
                    }
                }
            }
        }
    }

    public static void sys_ctl_pv_normal_noReflux(PvCtlDto pvCtlDto, ControlDto controlEMS, ControlDto controlPV) {
        if (OverallEMSData[14000] == 2 && pvCtlDto.getPv_power() != pvCtlDto.getPv_rated_power()){
            //设置光伏功率为最大功率
            controlPV.setSlaveId(1);
            controlPV.setAddress(368);
            controlPV.setValue(pvCtlDto.getPv_rated_power());
            MsgQueue.writeToControlPV(controlPV);
        } else{
            //时段校验算出功率
            AtomicInteger AtoPower_ESS = PV_Time_ctl(pvCtlDto);
            int Power_Time = AtoPower_ESS.intValue();
            if ((pvCtlDto.getSys_soc() <= pvCtlDto.getSoc_l() || pvCtlDto.getL_alarm() != 0) && Power_Time > 0) {
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
            } else if ((pvCtlDto.getSys_soc() >= pvCtlDto.getSoc_h() || pvCtlDto.getH_alarm() != 0) && Power_Time < 0) {
                controlEMS.setSlaveId(controlId);
                controlEMS.setAddress(302);
                controlEMS.setValue(0);
                MsgQueue.writeToControlTcp(controlEMS);
            } else {
                int Power = pvCtlDto.getGrid_power() + pvCtlDto.getSys_cmd() - pvCtlDto.getAllow_range();
                if (Power_Time > 0) {
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(302);
                    controlEMS.setValue(Power_Time);
                    MsgQueue.writeToControlTcp(controlEMS);
                } else {
                    if (Power < 0) {
                        int PESS = Math.max(Power, Power_Time);
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(PESS);
                        MsgQueue.writeToControlTcp(controlEMS);
                    } else {
                        controlEMS.setSlaveId(controlId);
                        controlEMS.setAddress(302);
                        controlEMS.setValue(0);
                        MsgQueue.writeToControlTcp(controlEMS);
                    }
                }
            }
        }
    }
    /*
     * 离网控制策略-AHLS-02专用（无电表）
     * */

    //    public void off_ToGrid_Nometer() {
//        String pvVSet = UtilFile.getFileItem("config.ini", "PV_V_SET");//pv侧电压
//        int pvSetV = Integer.parseInt(pvVSet);
//        String Selfstartflag = UtilFile.getFileItem("config.ini", "Self_start_flag");
//        int Self_start_flag = Integer.parseInt(Selfstartflag);
//        ParamDto param = SQLite3Utils.query();
//        int soc_h = Integer.parseInt(param.getSoc_h());
//        int soc_l = Integer.parseInt(param.getSoc_l());
//        int back_power = Integer.parseInt(param.getBack_power());
//        int allow_range = param.getGrid_alw();
//        int offgrid = param.getOffgrid_alw();//容差
//        String ESS_MAX_Str = UtilFile.getFileItem("config.ini", "ESS_MAX");
//        int ESS_MAX = Integer.parseInt(ESS_MAX_Str);//最大允许充放电
//        int offgrid_alw = ESS_MAX - offgrid;
//        ControlDto controlTcp = new ControlDto();
//        short meter_com_status = (short) GlobalParam.com2Fault;//电表通讯状态 0 表示正常 1 表示中断
//        //光伏控制器通讯状态
//        short pvC_status = (short) GlobalParam.com3Fault;//电表通讯状态 0 表示正常 1 表示中断
//        short sys_status = DataArray.data[1];//系统状态
//        short sys_ctl_status = DataArray.data[61];//系统控制状态
//        short sys_power = DataArray.data[6];//系统有功功率
//        short sys_soc = DataArray.data[26];//系统soc
//        short allow_discharge = DataArray.data[343];//允许放电功率限值
////        short allow_charge = DataArray.data[343];//允许充电功率限值
//        short allow_charge = 50;//允许充电功率限值
////        short sys_soc = 30;//系统soc
//        //short no_charge_mark = DataArray.data[4017];//禁充标志
//        short no_discharge_mark = DataArray.data[4018];//禁放标志
//        short PV_Power = DataArray.data[14003];//光伏输出功率
//        short PV_V = DataArray.data[14001];
////        short PV_Power = 6;//光伏输出功率
//        short PV_ctl_power = DataArray.data[368];//光伏控制功率
////        short PV_Max_Power = DataArray.data[14004];//光伏额定输出功率
//        short PV_Max_Power = (short) back_power;//光伏额定输出功率
////        short PV_Max_Power = 6;//光伏额定输出功率
//        short PCS_State = DataArray.data[127];//PCS状态
//
////        short L1_High2 = DataArray2.data[112];
////        short L1_low2 = DataArray2.data[113];
////        short L2_High2 = DataArray2.data[114];
////        short L2_low2 = DataArray2.data[115];
////        short L3_High2 = DataArray2.data[116];
////        short L3_low2 = DataArray2.data[117];
////        //负载功率
////        short Load_meter_power = HexUtils.Power(L1_High2, L1_low2, L2_High2, L2_low2, L3_High2, L3_low2);
//        //电表功率数据
//        short L1_High1 = DataArray2.data[12];
//        short L1_low1 = DataArray2.data[13];
//        short L2_High1 = DataArray2.data[14];
//        short L2_low1 = DataArray2.data[15];
//        short L3_High1 = DataArray2.data[16];
//        short L3_low1 = DataArray2.data[17];
//        short Grid_meter_power = (short) HexUtils.Power(L1_High1, L1_low1, L2_High1, L2_low1, L3_High1, L3_low1);
////        tcplog.info("离网模式-系统功率" + sys_power * 0.1 + "光伏功率" + PV_Power * 0.1 + "电网功率" + Grid_meter_power * 0.1);
//        //判断系统是否为运行
//        if (sys_status == 3) {//系统故障
//            //给系统下发停机
//            controlTcp.setSlaveId(16);
//            controlTcp.setAddress(GlobalParam.onoff);
//            controlTcp.setValue(GlobalParam.stop);
//            MsgQueue.writeToControlTcp(controlTcp);
//        } else {
//            //出现低压告警或者soc<socl
//            if (sys_soc < soc_l) {
////                tcplog.info("soc < socl");
//                if (no_discharge_mark == 1) {
//                    //给系统下发停机
//                    controlTcp.setSlaveId(16);
//                    controlTcp.setAddress(GlobalParam.onoff);
//                    controlTcp.setValue(GlobalParam.stop);
//                    MsgQueue.writeToControlTcp(controlTcp);
//                } else {
//                    if (Grid_meter_power != 0) {
//                        if (sys_ctl_status != 6) {
//                            //给系统下发自启动
//                            controlTcp.setSlaveId(16);
//                            controlTcp.setAddress(GlobalParam.onoff);
//                            controlTcp.setValue(GlobalParam.start);
//                            MsgQueue.writeToControlTcp(controlTcp);
//                        }
//                    } else {
//                        if (sys_ctl_status == 6) {
//                            int SYS_POWER1 = Math.abs(sys_power);
//                            int PV_POWER1 = PV_Power + sys_power + offgrid_alw;
//                            if (PV_POWER1 < 0) {
//                                //给光伏下发限制功率
//                                controlTcp.setSlaveId(1);
//                                controlTcp.setAddress(368);
//                                controlTcp.setValue(0);
//                                MsgQueue.writeToControlPV(controlTcp);
////                                    System.out.println("PV" + PV_Power + "SYS" + sys_power + "差值" + allow_range);
//                            } else if (PV_POWER1 > PV_Max_Power) {
//                                //给光伏下发限制功率
//                                controlTcp.setSlaveId(1);
//                                controlTcp.setAddress(368);
//                                controlTcp.setValue((int) PV_Max_Power);
//                                MsgQueue.writeToControlPV(controlTcp);
////                                System.out.println("PV" + PV_Power + "SYS" + sys_power + "---1");
//                            } else {
//                                //给光伏下发限制功率
//                                controlTcp.setSlaveId(1);
//                                controlTcp.setAddress(368);
//                                controlTcp.setValue(PV_POWER1);
//                                MsgQueue.writeToControlPV(controlTcp);
////                                System.out.println("PV" + PV_Power + "SYS" + sys_power + "---2");
//                            }
////                            }
//                        }
//                    }
//                }
//            } else {
//                if (sys_status == 2) {
//                    if (sys_soc >= soc_h) {
////                        tcplog.info("soc >= soch");
//                        GlobalParam.bansign = 1;
//                        int PV_POWER2 = PV_Power + sys_power - offgrid;
//                        if (PV_POWER2 < 0) {
//                            //给光伏下发限制功率
//                            controlTcp.setSlaveId(1);
//                            controlTcp.setAddress(368);
//                            controlTcp.setValue(0);
//                            MsgQueue.writeToControlPV(controlTcp);
//                        } else if (PV_POWER2 > PV_Max_Power) {
//                            controlTcp.setSlaveId(1);
//                            controlTcp.setAddress(368);
//                            controlTcp.setValue((int) PV_Max_Power);
//                            MsgQueue.writeToControlPV(controlTcp);
//                        } else {
//                            controlTcp.setSlaveId(1);
//                            controlTcp.setAddress(368);
//                            controlTcp.setValue(PV_POWER2);
//                            MsgQueue.writeToControlPV(controlTcp);
//                        }
//
//                    } else {
//                        if (GlobalParam.bansign == 1) {
//                            if (sys_soc < soc_h) {
////                                tcplog.info("soc < soch");
//                                GlobalParam.bansign = 0;
//                                int SYS_POWER3 = Math.abs(sys_power);
//                                int PV_POWER3 = PV_Power + sys_power + offgrid_alw;
//                                if (SYS_POWER3 > offgrid_alw) {
//                                    if (PV_POWER3 < 0) {
//                                        //给光伏下发限制功率
//                                        controlTcp.setSlaveId(1);
//                                        controlTcp.setAddress(368);
//                                        controlTcp.setValue(0);
//                                        MsgQueue.writeToControlPV(controlTcp);
////                                    System.out.println("PV" + PV_Power + "SYS" + sys_power + "差值" + allow_range);
//                                    } else if (PV_POWER3 > PV_Max_Power) {
//                                        //给光伏下发限制功率
//                                        controlTcp.setSlaveId(1);
//                                        controlTcp.setAddress(368);
//                                        controlTcp.setValue((int) PV_Max_Power);
//                                        MsgQueue.writeToControlPV(controlTcp);
//                                    } else {
//                                        //给光伏下发限制功率
//                                        controlTcp.setSlaveId(1);
//                                        controlTcp.setAddress(368);
//                                        controlTcp.setValue(PV_POWER3);
//                                        MsgQueue.writeToControlPV(controlTcp);
//                                    }
//                                }
//                            }
//                        } else {
//                            int SYS_POWER4 = Math.abs(sys_power);
//                            int PV_POWER4 = PV_Power + sys_power + offgrid_alw;
//                            if (SYS_POWER4 > offgrid_alw) {
//                                if (PV_POWER4 < 0) {
//                                    //给光伏下发限制功率
//                                    controlTcp.setSlaveId(1);
//                                    controlTcp.setAddress(368);
//                                    controlTcp.setValue(0);
//                                    MsgQueue.writeToControlPV(controlTcp);
////                                    System.out.println("PV" + PV_Power + "SYS" + sys_power + "差值" + allow_range);
//                                } else if (PV_POWER4 > PV_Max_Power) {
//                                    //给光伏下发限制功率
//                                    controlTcp.setSlaveId(1);
//                                    controlTcp.setAddress(368);
//                                    controlTcp.setValue((int) PV_Max_Power);
//                                    MsgQueue.writeToControlPV(controlTcp);
//                                } else {
//                                    //给光伏下发限制功率
//                                    controlTcp.setSlaveId(1);
//                                    controlTcp.setAddress(368);
//                                    controlTcp.setValue(PV_POWER4);
//                                    MsgQueue.writeToControlPV(controlTcp);
//                                }
//                            }
//                        }
//                    }
//                } else if (sys_status == 1 && Self_start_flag == 1) {
//                    //给系统下发自启动
//                    controlTcp.setSlaveId(16);
//                    controlTcp.setAddress(GlobalParam.onoff);
//                    controlTcp.setValue(GlobalParam.start);
//                    MsgQueue.writeToControlTcp(controlTcp);
//                }
//            }
//        }
//    }
    public static void PV_Uncontroll_ESS(ControlDto controlEMS) {
//        PvCtlDto pvCtlDto = new PvCtlDto();
//        ParamDto param = SQLiteParamUtils.query();
//        int soc_h = Integer.parseInt(param.getSoc_h());
//        int soc_l = Integer.parseInt(param.getSoc_l());
//        int pv_max = param.getPv_max() * 10;
//        int stepSize = param.getStep_size() * 10;
//        int allowance = param.getGrid_alw() * 10;
//        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
//        int Tolerance_Boundary = 50;//容差边界
//        double PV_Rate = 0.5;//PV变化率
//        //光伏控制器通讯状态
//        short sys_status = OverallEMSData[1];//系统状态
//        short sys_ctl_status = OverallEMSData[61];//系统控制状态
//        short sys_power = OverallEMSData[63];//PHC系统有功功率
//        short sys_soc = OverallEMSData[26];//系统soc
//        //电网功率
//        short Grid_meter_power = ammeterUtils.getGrid_Power();
//        //允许充电
//        pvCtlDto.setAllow_charge(OverallEMSData[5]);
//        //允许放电
//        pvCtlDto.setAllow_discharge(OverallEMSData[4]);
//        //电网功率
//        pvCtlDto.setGrid_power(Grid_meter_power);
////        //高压保护
//        pvCtlDto.setH_alarm(OverallEMSData[16]);
////        //低压保护
//        pvCtlDto.setL_alarm(OverallEMSData[17]);
//        //光伏功率
//        pvCtlDto.setPv_power(OverallEMSData[368]);
//        //光伏额定功率
//        pvCtlDto.setPv_rated_power(pv_max);
//        //系统soc
//        pvCtlDto.setSys_soc(OverallEMSData[26]);
////        pvCtlDto.setSys_soc(30);
//        //系统指令功率
//        pvCtlDto.setSys_cmd(OverallEMSData[63]);
//        //容差
//        pvCtlDto.setAllow_range(allowance);
//        //soc_h
//        pvCtlDto.setSoc_h(soc_h);
//        //soc_l
//        pvCtlDto.setSoc_l(soc_l);
//        //步长
//        pvCtlDto.setStep_size(stepSize);
//
//        if (sys_status == 1 && meter_com_status != 1 && sys_ctl_status != 3) {
//            if (pvCtlDto.getGrid_power() < pvCtlDto.getAllow_range() - Tolerance_Boundary) {
//                int power = pvCtlDto.getSys_cmd() + pvCtlDto.getGrid_power() - pvCtlDto.getAllow_range();
//                controlEMS.setSlaveId(controlId);
//                controlEMS.setAddress(302);
//                controlEMS.setValue(power);
//                MsgQueue.writeToControlTcp(controlEMS);
//            } else {
//                if (pvCtlDto.getGrid_power() > pvCtlDto.getAllow_range() + Tolerance_Boundary) {
//                    int power = pvCtlDto.getSys_cmd() - pvCtlDto.getGrid_power() + pvCtlDto.getAllow_range();
//                    controlEMS.setSlaveId(controlId);
//                    controlEMS.setAddress(302);
//                    controlEMS.setValue(power);
//                    MsgQueue.writeToControlTcp(controlEMS);
//                } else {
//                    if (pvCtlDto.getGrid_power() - 2 * Tolerance_Boundary > 0) {
//                        double rate = /stepSize
//
//                    }
//                }
//            }
//        }
    }

    /*
     * 并网策略-断路器
     * */
    public static void on_Grid_breaker(ControlDto controlEMS, ControlDto controlPV) {
        short meter_com_status = Communication.ammeter_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        //光伏控制器通讯状态
        short pvC_status = Communication.pvm_interrupt;//电表通讯状态 0 表示正常 1 表示中断
        short sys_status = OverallEMSData[1];//系统状态
        short sys_ctl_status = OverallEMSData[61];//系统控制状态
        short sys_power = OverallEMSData[63];//系统有功功率
        short sys_soc = OverallEMSData[26];//系统soc
        short allow_Power = OverallEMSData[4];
        ParamDto param = SQLiteParamUtils.query();
        int soch = Integer.parseInt(param.getSoc_h());
        int socl = Integer.parseInt(param.getSoc_l());
        int backpower = Integer.parseInt(param.getBack_power());
        int allowance = param.getGrid_alw();
        //soc_h(数据库查询)
        int soc_h = soch;
        //soc_l(数据库查询)
        int soc_l = socl;
        //容差
        int allow_range = allowance;

        //光伏断路器状态
        short PV_break_statu = OverallEMSData[366];
        short no_charge_mark = OverallEMSData[16];//禁充标志
        short no_discharge_mark = OverallEMSData[17];//禁放标志
        short PV_Power = OverallEMSData[14003];//光伏实际输出功率
        short PV_ctl_power = OverallEMSData[368];//光伏控制功率
        short PV_Max_Power = OverallEMSData[14004];//光伏额定输出功率
        short Grid_meter_power = ammeterUtils.getGrid_Power();
        System.out.println("调节时间" + System.currentTimeMillis() + "电网功率:" +
                Grid_meter_power * 0.1 + " 系统功率:" + sys_power * 0.1);

        //判断通讯中断
        if (meter_com_status == 0 && pvC_status == 0) {
            //判断系统状态
            if ((sys_status == 2 && sys_ctl_status == 6)) {
                //判断系统SOC的范围
                if (sys_soc > soc_h || no_charge_mark == 1) {
                    //断开光伏断路器
                    if (PV_break_statu != 1) {
                        controlPV.setSlaveId(1);
                        controlPV.setAddress(366);
                        controlPV.setValue(1);
                        MsgQueue.writeToControlPV(controlPV);
                    }
                } else if (sys_soc < soc_l || no_discharge_mark == 1) {
                    //闭合光伏断路器
                    if (PV_break_statu != 0) {
                        controlPV.setSlaveId(1);
                        controlPV.setAddress(366);
                        controlPV.setValue(0);
                        MsgQueue.writeToControlPV(controlPV);
                    }
                }
                if (Grid_meter_power != allow_range) {
                    int power = (sys_power + Grid_meter_power - allow_range);
                    //取绝对值，然后取最小值
                    int min = Math.min(Math.abs(power), allow_Power);
                    if (power >= 0) {
                        if (min != sys_power) {
                            controlEMS.setSlaveId(controlId);
                            controlEMS.setAddress(302);
                            controlEMS.setValue(min);
                            MsgQueue.writeToControlTcp(controlEMS);
                            DataArray.data[63] = (short) min;
                        }
                    } else {
                        if ((-min) != sys_power) {
                            controlEMS.setSlaveId(controlId);
                            controlEMS.setAddress(302);
                            controlEMS.setValue(-min);
                            MsgQueue.writeToControlTcp(controlEMS);
                            DataArray.data[63] = (short) (-min);
                        }
                    }
                }
            } else if (sys_status == 1) {
                if (sys_ctl_status != 6) {
                    //给系统下发启动
                    controlEMS.setSlaveId(controlId);
                    controlEMS.setAddress(GlobalParam.onoff);
                    controlEMS.setValue(GlobalParam.start);
                    MsgQueue.writeToControlTcp(controlEMS);
                }
                //系统故障或者系统停止
            } else if (sys_status == 3) {
                //断开光伏
                if (PV_break_statu != 1) {
                    controlPV.setSlaveId(1);
                    controlPV.setAddress(366);
                    controlPV.setValue(1);
                    MsgQueue.writeToControlPV(controlPV);
                }
            }
        }
    }


    /*
     *光伏时段
     **/
    @PostMapping("add")
    public Result<Object> creatPV(@RequestBody ArrayList<PVTimeDto> list) {
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                PVTimeDto pvTimeDto = list.get(i);
                pvTimeDto.setId(i + 1);
                if (pvTimeDto.getRunState() == 1) {
                    pvTimeDto.setPower(-pvTimeDto.getPower());
                }
                SQLPVTimeUtils.updateToDB(pvTimeDto);
            }
        }
        return Result.success(new ArrayList<>());
    }

    /*
     *
     * */
    @PostMapping("query")
    public List<PVTimeDto> queryDB() {
        return SQLPVTimeUtils.query();
    }

}

